package co.edu.javeriana.konigsberg.xavier.dao;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import co.edu.javeriana.konigsberg.xavier.dao.desc.ProgramaDAOLocal;
import co.edu.javeriana.konigsberg.xavier.entidad.Programa;

@Stateless
public class ProgramaDAO extends DAO<Programa> implements ProgramaDAOLocal{
	private static final long serialVersionUID = 1L;
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Programa> obtenerLista(List<String> estados){
		return (List<Programa>) em.createNamedQuery("Programa.findByStates").setParameter("estados", estados).getResultList();
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Programa> obtenerLista(List<String> estados, BigInteger idAreaGestion, BigInteger idCategoria){
		//if(idAreaGestion != null){
			int paraNum = 3;
			String in = "";
			for (int i = 0; i < estados.size(); i++) {
				in += "?" + paraNum++ +", ";
			}
			in = in.substring(0, in.lastIndexOf(","));
			String sql = "SELECT DISTINCT P.idPrograma FROM programa P "
					+ "JOIN programadepartamentofacultad PDF ON PDF.idPrograma = P.idPrograma "
					+ "JOIN departamentofacultad DF ON DF.idDepartamentoFacultad = PDF.idDepartamentoFacultad "
					+ "JOIN facultad F ON F.idFacultad = DF.idFacultad "
					+ "JOIN areagestion AG ON AG.idAreaGestion = F.idAreaGestion "
					+ "JOIN categoria c ON c.idCategoria = P.idCategoria "
					+ "WHERE 1 = 1 "
					+ (idAreaGestion != null ? "AND AG.idAreaGestion = ?1 " : "")
					+ (idCategoria != null ? "AND c.idCategoria = ?2 " : "")
					+ "AND P.estado IN (" + in + ") ";
			Query query = em.createNativeQuery
					(sql);
			if(idAreaGestion != null){
				query.setParameter(1, idAreaGestion);
			}
			if(idCategoria != null){
				query.setParameter(2, idCategoria);
			}
			paraNum = 3;
			for (int i = 0; i < estados.size(); i++) {
				query.setParameter(paraNum++, estados.get(i));
			}
			
			List idsProgramas = query.getResultList();
			List<Programa> programas = new ArrayList<Programa>();
			for (int i = 0; i < idsProgramas.size(); i++) {
				Object idProg = idsProgramas.get(i);
				Programa programa = obtenerPorId(new BigInteger(idProg.toString()));
				programas.add(programa);
				System.out.println();
			}
			return programas;
//		}else{
//			return obtenerLista(estados);
//		}
		
	    
	}

	@Override
	public List<Programa> obtenerListaPorPadre(BigInteger idPadre) {
		return null;
	}

	@Override
	public Programa obtenerPorId(BigInteger id) {
		if(id != null){
		return em.find(Programa.class, id);
		}
		return null;
	}

	@Override
	public void guardar(Programa programa) {
		EntityTransaction tx = em.getTransaction();
		tx.begin();
		em.persist(programa);
		tx.commit();
		
	}

	@Override
	public void actualizar(Programa programa) {
		EntityTransaction tx = em.getTransaction();
		tx.begin();
		em.merge(programa);
		tx.commit();
		
	}

	@Override
	public List<Programa> obtenerLista() {
		return (List<Programa>) em.createNamedQuery("Programa.findAll").getResultList();
	}

	
}
